package com.factorit.business;

import java.lang.reflect.InvocationTargetException;

import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;

import org.apache.commons.beanutils.PropertyUtils;

public class JSonTranslator {

	public static String getSerializedObject(Object obj) {
		return JSONSerializer.toJSON(obj).toString();
	}

	public static String getSerializedRelation(Object obj) {
		JSONArray jArray = new JSONArray();
		try {
			jArray.element(0, String.valueOf(PropertyUtils.getNestedProperty(obj, "id")));
			jArray.element(1, String.valueOf(PropertyUtils.getNestedProperty(obj, "value")));
			jArray.element(2, String.valueOf(PropertyUtils.getNestedProperty(obj, "type")));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}

		return jArray.toString();
	}

}
